package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.enums.GlobalEnv;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.handler.InterfaceHandler;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("all")
@MarkAsApi
public class AllInterface extends InterfaceHandler {
    @Override
    public Object execute(Map<String, Object> paramsMap) {
        String key = "cache@allInterface";
        String cache = redisUtils.getString(key);
        List<Map<String, Object>> apis;
        if (Verification.checkNotNull(cache)) {
            apis = DictMap.parseObject(cache, new DictMap.JavaType<List<Map<String, Object>>>() {
            });
        } else {
            String getInterface = """
                    select t1.sequence,interface_name,replace(params,',"require":',',"required":') params,comments,check_permission,t1.method,case when return_type='auto' then 'list|map' else return_type end return_type,
                        t1.url,case when type='sql' then 1 else 0 end page,response
                        from x_interface_info t1\s
                    where t1.api_type=0 and t1.status=1""";
            apis = jdbcTemplate.queryForList(getInterface);
            redisUtils.set(key, Transformation.toJSONString(apis), 3, TimeUnit.MINUTES);
        }

        for (Map<String, Object> api : apis) {
            String apiRunCount = redisUtils.getString((GlobalEnv.InterfaceTotalCountPrefix + "@" + api.get("sequence")));
            api.put("count", Transformation.nvl(apiRunCount, "0"));
        }
        return apis;
    }
}